package com.yunbiao.ybbusstopplate.utils.core;

import android.text.TextUtils;
import android.util.Log;


import com.blankj.utilcode.util.ActivityUtils;
import com.yunbiao.ybbusstopplate.base.APP;
import com.yunbiao.ybbusstopplate.base.Constants;
import com.yunbiao.ybbusstopplate.base.HeartBeatClient;
import com.yunbiao.ybbusstopplate.utils.CommonUtils;
import com.yunbiao.ybbusstopplate.utils.SpUtils;
import com.yunbiao.ybbusstopplate.utils.xutil.MyXutils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by LiuShao on 2016/3/4.
 */

public class MachineDetial {
    private static final String TAG = "MachineDetial";
    private String upMechineDetialUrl = Constants.RESOURCE_URL + "device/service/updateDeviceHardwareInfo.html";

    private static MachineDetial machineDetial;

    public static MachineDetial getInstance() {
        if (machineDetial == null) {
            machineDetial = new MachineDetial();
        }
        return machineDetial;
    }

    private MachineDetial() {
        DeviceLocation.getDeviceLocation().getLocation();
    }

    private LocationBean LocationBean;
    private boolean isLocationInited = false;

    public void setLocation(LocationBean LocationBean) {
        this.LocationBean = LocationBean;
        isLocationInited = true;
        upLoadHardWareMessage();
        DeviceLocation.getDeviceLocation().stopLocation();
    }

    public LocationBean getLocation() {
//        isLocationInited = true;
//        upLoadHardWareMessage();
//        DeviceLocation.getDeviceLocation().stopLocation();
        if (LocationBean==null){
            LocationBean=new LocationBean();
        }
        return LocationBean;
    }

    /**
     * 上传设备信息
     */
    public void upLoadHardWareMessage() {
        HandleMessageUtils.getInstance().runInThread(new Runnable() {
            @Override
            public void run() {
                Map<String, String> map = new HashMap<String, String>();
                map.put("deviceNo", HeartBeatClient.getDeviceNo());
                map.put("screenWidth", String.valueOf(CommonUtils.getScreenWidth(APP.Companion.getAppContext())));
                map.put("screenHeight", String.valueOf(CommonUtils.getScreenHeight(APP.Companion.getAppContext())));
                map.put("diskSpace", CommonUtils.getMemoryTotalSize());
                map.put("useSpace", CommonUtils.getMemoryUsedSize());
                map.put("softwareVersion", CommonUtils.getAppVersion(APP.Companion.getAppContext()) + "_" + VersionUpdateConstants
                        .CURRENT_VERSION);
                map.put("screenRotate", String.valueOf(CommonUtils.getDisplayRotation(ActivityUtils.getTopActivity())));
                map.put("deviceCpu", CommonUtils.getCpuName() + " " + CommonUtils.getNumCores() + "核" + CommonUtils
                        .getMaxCpuFreq() + "khz");
                map.put("deviceIp", CommonUtils.getIpAddress());//当前设备IP地址
                map.put("mac", CommonUtils.getLocalMacAddress());//设备的本机MAC地址
                map.put("wirelessMac", CommonUtils.getWifiMacAddress());//设备的wifi MAC地址


                map.put("hardwareType",android.os.Build.MODEL);//设备型号
                map.put("androidVersion",android.os.Build.VERSION.RELEASE);//安卓版本号
                map.put("deviceVersion",android.os.Build.DISPLAY);//内部版本号

                if (isLocationInited&&LocationBean!=null) {
                    String latitude = LocationBean.getAltitude();
                    if (!TextUtils.isEmpty(latitude)){
                        map.put("latitude", latitude);
                    }
                    String longitude = LocationBean.getLongitude();
                    if (!TextUtils.isEmpty(longitude)){
                        map.put("longitude", longitude);
                    }
                    String adress = LocationBean.getAdress();
                    if (!TextUtils.isEmpty(adress)){
                        map.put("address", adress);
                    }

                    String adressHeight = LocationBean.getAdressHeight();
                    if (!TextUtils.isEmpty(adressHeight)){
                        map.put("addressHeight",adressHeight);
                    }

                    String city = LocationBean.getCity();
                    if (!TextUtils.isEmpty(city)){
                        map.put("cityName", city);
                        //定位后在SharedPreferences存入定位得到的城市名字，后边获取。重启后定位覆盖
                        SpUtils.saveString(APP.Companion.getAppContext(), SpUtils.CITY_NAME, city);
                    }
                }
                Log.e(TAG, "run-------upMechineDetialUrl---------->"+upMechineDetialUrl );
                Log.d("post", "updateDeviceHardwareInfo 提交： " + map);
                MyXutils.getInstance().post(upMechineDetialUrl, map, new MyXutils.XCallBack() {
                    @Override
                    public void onSuccess(String result) {
                        Log.e(TAG, "onSuccess:---------upLoadHardWareMessage-----------> "+result );
                    }

                    @Override
                    public void onError(Throwable ex) {
                        Log.e(TAG, "onError:---------upLoadHardWareMessage-----------> "+ex.getMessage() );
                    }

                    @Override
                    public void onFinish() {

                    }
                });
            }
        });
    }
}
